
import json
import re
from consumer_price_index.items import ConsumerPriceIndexItem
from scrapy.linkextractors import LinkExtractor
import scrapy
from lxml import etree
class CPISpider(scrapy.Spider):
    name = "c_p_i"
    allowed_domains = ["eastmoney.com"]
    start_urls = ["https://data.eastmoney.com/cjsj/cpi.html"]
#邓春梅爬取财富网cpi
#2022.8--2024.3
#杨慧林数据库建表
    def parse(self, response):

        item =ConsumerPriceIndexItem()
        html = etree.HTML(response.text)



        rows = html.xpath('//*[@id="cjsj_table"]/table/tbody/tr')
        for row in rows:
            each = row.xpath('.//td')
            item['detail_time'] =each[0].xpath('./text()')
            item['country_current_month'] =each[1].xpath('./text()')
            item['country_year_growth'] =each[2].xpath('.//span/text()')
            item['country_seq_growth'] =each[3].xpath('.//span/text()')
            item['country_total'] =each[4].xpath('./text()')
            item['city_current_month'] =each[5].xpath('./text()')
            item['city_year_growth'] =each[6].xpath('.//span/text()')
            item['city_seq_growth'] =each[7].xpath('.//span/text()')
            item['city_total'] =each[8].xpath('./text()')
            item['village_current_month']=each[9].xpath('./text()')
            item['village_year_growth'] =each[10].xpath('.//span/text()')
            item['village_seq_growth'] =each[11].xpath('.//span/text()')
            item['village_total'] =each[12].xpath('./text()')
            yield item
            # print(item['detail_time'])




